{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3636e81a",
   "metadata": {},
   "source": [
    "# 百度AI开放平台"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "252043a5",
   "metadata": {},
   "source": [
    "## 通用物体的场景识别"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d64016ed",
   "metadata": {},
   "source": [
    "> 接口描述\n",
    "> 该请求用于通用物体及场景识别，即对于输入的一张图片（可正常解码，且长宽比适宜），输出图片中的多个物体及场景标签。\n",
    "> 请求示例"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75339eba",
   "metadata": {},
   "source": [
    "### 鉴权认证机制"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee39a887",
   "metadata": {},
   "source": [
    "> 获取Access Token\n",
    "> 获取access_token示例代码:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "10d75c80",
   "metadata": {},
   "outputs": [],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官网获取的AK】&client_secret=【官网获取的SK】'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6377e84d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.67d908bf9bcb2f22a5c05c0d47f9123a.315360000.1963917414.282335-25869159', 'expires_in': 2592000, 'session_key': '9mzdDAE/vaVDTodZLomMHMWfqS7pv+uRP1nawbTSh0I95wrseV/1yPNu15fIYg6+3bYzYeXy8Wgqvrf3cLebVE0dLR3R6A==', 'access_token': '24.7401978bc62ed23f2ed1e96bf0eddadb.2592000.1651149414.282335-25869159', 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage brain_multi_ object_detect wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': 'd6437a5025e39245c6dd323f777b3eb6'}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'ldCbqycq23IzQVSCENC4VmKf',\n",
    "    'client_secret':'5OF7nRbC3B9B1VMm2Vn95r4iEwrRdj6u'\n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "if response:\n",
    "    print(response.json())\n",
    "    access_token = response.json()['access_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6f041efa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.7401978bc62ed23f2ed1e96bf0eddadb.2592000.1651149414.282335-25869159'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "access_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0ad7b723",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97686f9d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "880a0116",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>参数</th>\n",
       "      <th>是否必选</th>\n",
       "      <th>类型</th>\n",
       "      <th>可选值范围</th>\n",
       "      <th>说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>image</td>\n",
       "      <td>和url二选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>图像数据，base64编码，要求base64编码后大小不超过4M，最短边至少15px，最长边...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>url</td>\n",
       "      <td>和image二选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>图片完整URL，URL长度不超过1024字节，URL对应的图片base64编码后大小不超过4...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>baike_num</td>\n",
       "      <td>否</td>\n",
       "      <td>integer</td>\n",
       "      <td>-</td>\n",
       "      <td>返回百科信息的结果数，默认不返回</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          参数       是否必选       类型 可选值范围  \\\n",
       "0      image    和url二选一   string     -   \n",
       "1        url  和image二选一   string     -   \n",
       "2  baike_num          否  integer     -   \n",
       "\n",
       "                                                  说明  \n",
       "0  图像数据，base64编码，要求base64编码后大小不超过4M，最短边至少15px，最长边...  \n",
       "1  图片完整URL，URL长度不超过1024字节，URL对应的图片base64编码后大小不超过4...  \n",
       "2                                   返回百科信息的结果数，默认不返回  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 图像识别 url ： https://ai.baidu.com/ai-doc/IMAGERECOGNITION/Xk3bcxe21\n",
    "pd.read_html('https://ai.baidu.com/ai-doc/IMAGERECOGNITION/Xk3bcxe21')[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "dc299d8d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'keyword': '阁楼', 'score': 0.762345, 'root': '商品-家具', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E9%98%81%E6%A5%BC/3440', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/bd3eb13533fa828bd4b89c77f71f4134960a5a8e', 'description': '阁楼，即指位于房屋坡屋顶下部的房间，是一种建筑结构，就是在楼房的空间比较高，在中间再重新制作一层阁楼楼板。阁楼一般用于储藏、办公、住人等的私密空间。'}}, {'keyword': '建筑', 'score': 0.601532, 'root': '建筑-现代建筑', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E5%BB%BA%E7%AD%91/405099', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/6159252dd42a283431973c7454b5c9ea15cebf9f', 'description': '指人工建筑而成的资产，属于固定资产范畴，包括房屋和构筑物两大类。房屋是指供人居住、工作、学习、生产、经营、娱乐、储藏物品以及进行其他社会活动的工程建筑。与建筑物有区别的是构筑物，构筑物指房屋以外的工程建筑，如围墙、道路、水坝、水井、隧道、水塔、桥梁和烟囱等。'}}, {'keyword': '楼梯', 'score': 0.44034, 'root': '建筑-居家室内', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E6%A5%BC%E6%A2%AF/1739259', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/0bd162d9f2d3572cff10982a8213632763d0c352', 'description': '建筑物中作为楼层间垂直交通用的构件。用于楼层之间和高差较大时的交通联系。在设有电梯、自动梯作为主要垂直交通手段的多层和高层建筑中也要设置楼梯。高层建筑尽管采用电梯作为主要垂直交通工具，但仍然要保留楼梯供火灾时逃生之用。楼梯由连续梯级的梯段(又称梯跑)、平台(休息平台)和围护构件等组成。楼梯的最低和最高一级踏步间的水平投影距离为梯长，梯级的总高为梯高。中国战国时期铜器上的重屋形象中已镌刻有楼梯。15～16世纪的意大利，将室内楼梯从传统的封闭空间中解放出来，使之成为形体富于变化带有装饰性的建筑组成部分。'}}, {'keyword': '酒店大厅', 'score': 0.280702, 'root': '建筑-酒店', 'baike_info': {}}, {'keyword': '棚桥', 'score': 0.108449, 'root': '建筑-其他', 'baike_info': {}}], 'log_id': 1508786950173954584}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用物体和场景识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f = open('tu.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2. 酬载准备\n",
    "payload={\n",
    "    'access_token':access_token,\n",
    "    'image':img,\n",
    "    'baike_num':5\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f00e84f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "菜品识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/dish\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('yuxiangrousi.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "\n",
    "access_token = zhichao_AT\n",
    "payload={\n",
    "    'access_token':zhichao_AT,\n",
    "    'image':img,\n",
    "    'baike_num':5,\n",
    "    \"top_num\":5\n",
    "}\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
